package com.imooc.o2o.util;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 徐老板
 * @date 2020/1/5   9:17
 */
public class HttpServletRequestUtil {
    /**
     * 各种数据类型的转换
     * @param request
     * @param key
     * @return
     */

    public static int getInt(HttpServletRequest request,String key){
        try {
            Integer decode = Integer.decode(request.getParameter(key));
            return decode;
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return -1;
        }
    }


    public static long getLong(HttpServletRequest request,String key){
        try {
            return Long.valueOf(request.getParameter(key));
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return -1L;
        }
    }

    public static double getDouble(HttpServletRequest request,String key){
        try {
            return Double.valueOf(request.getParameter(key));
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return -1d;
        }
    }

    public static boolean getBoolean(HttpServletRequest request,String key){
        try {
            return Boolean.valueOf(request.getParameter(key));
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return false;
        }
    }


//这里面转字符串多了一个要去掉两端空格的问题
    public static String getString(HttpServletRequest request,String key){
        try {
            String result=request.getParameter(key);
            if (result!=null){
                result= result.trim();
            }
            if ("".equals(result)){
                result=null;
            }
            return result;
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return null;
        }

    }
}
